const { saveMessage, getHistoryMessage, setMessageStatus } = require("../controllers/message.controller");
const { addFriend } = require("../controllers/friend.controller");
const { serverAddConversationList } = require("../controllers/user.controller");
const socketIo = require("socket.io");
const tools = require("../utils/tools");
const appSocket = (server) => {
  console.log(server);
  let io = socketIo(server, {
    transports: ["websocket"]
  });
  const OnlineUser = {};
  //监听connect事件
  io.on("connection", async (socket) => {
    console.log("socket建立连接了");
    socket.emit("connection", "socket连接成功");
    //获取历史信息
    socket.on("getHisMeg", async (obj) => {
      let result = {};
      try {
        console.log(obj, "getHisMeg");
        result = await getHistoryMessage(obj, 1);
        console.log();
      } catch (e) {
        console.log(e);
      }
      socket.emit("getHisMeg", result);
    });
    //保存消息
    socket.on("sendValidate", async (obj) => {
      await saveMessage(obj);
      // 发送验证消息
      socket.to(obj.roomId).emit("takeValidate", obj);
    });
    socket.on("agree", async (obj) => {
      if (obj.state === "friend") {
        console.log(obj);
        const {
          self,
          otherName,
          otherAvatar,
          roomId,
          avatar,
          friendRoom,
          userName,
          otherUserName
        } = obj;
        const result = await addFriend(obj);
        if (result.code === 200) {
          const args = {
            status: "1",
            self
          };
          await setMessageStatus(args);

          const mesObj = {
            userName: "",
            mes: otherName + "同意了你的好友请求！",
            time: tools.formatTime(new Date()),
            avatar: otherAvatar,
            nickname: otherName,
            read: [],
            state: "friend",
            type: "info",
            status: "1", // 同意
            roomId: self + "-" + roomId.split("-")[1]
          };

          await saveMessage(mesObj);

          // 申请人信息
          const selfObj = {
            userName: otherUserName,
            avatar: otherAvatar,
            id: friendRoom,
            type: "friend",
            friendId: roomId.split("-")[0]
          };
          const otherObj = {
            // 好友信息
            userName: userName,
            avatar: avatar,
            id: friendRoom,
            type: "friend",
            friendId: self
          };

          await serverAddConversationList(userName, selfObj);
          await serverAddConversationList(otherUserName, otherObj);

          socket.to(mesObj.roomId).emit("takeValidate", mesObj);
          const org = {
            type: "org",
            nickname: otherUserName,
            time: tools.formatTime(new Date()),
            roomId: friendRoom
          };

          await saveMessage(org);

          socket.to(friendRoom).emit("org", org);
          socket.emit("ValidateSuccess", { msg: "ok" });
        }
      }
    });
  });
};
module.exports = appSocket;
